package com.frxuexi.user.repository

import com.frxuexi.lib_base.room.AskDao
import com.frxuexi.lib_base.room.AskLeave
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

@OptIn
class AskRepository @Inject constructor(private val askDao: AskDao) {
    fun askFor(askLeave: AskLeave): Flow<Long> {
        return flow { emit(askDao.insert(askLeave)) }
    }

    fun askListByUid(uid:Int): Flow<List<AskLeave>> {
        return flow { emit(askDao.selectByUid(uid)) }
    }

    suspend fun askListByPage(uid:Int,page:Int,size:Int): List<AskLeave> {
        return withContext(Dispatchers.IO){
            askDao.selectByIdPage(uid,page,size)
        }
    }

    suspend fun getCount(uid:Int): Int {
        return withContext(Dispatchers.IO){
            askDao.getCount(uid)
        }
    }

    fun selectByNoApprove(): Flow<List<AskLeave>> {
        return flow { emit(askDao.selectByNoApprove()) }
    }

    fun approve(askLeave: AskLeave):Flow<Int>{
        return flow { emit(askDao.update(askLeave = askLeave)) }
    }


}